package com.gss.common.service;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;

@Service
public class MailService {
	
	@Autowired
	private JavaMailSender mailSender;
	
    private Logger logger = Logger.getLogger(MailService.class);

    public void setMailSender(JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }

	public boolean sendMail(final String emailId,final String text)
	{
		logger.debug("inside method(sendMail)");
		MimeMessagePreparator preparator = new MimeMessagePreparator() {
    		public void prepare(MimeMessage mimeMessage) throws Exception {
    		MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
    		InternetAddress internetAddress = new InternetAddress(emailId);
    		messageHelper.setTo(internetAddress);
    		logger.debug("internetAddress: " + internetAddress);
    		messageHelper.setFrom("ArmoryAdministrator");
    		messageHelper.setText(text,true);
    		messageHelper.setSubject("Requested Document's Password");
    		}
    		};
    		try {
    		this.mailSender.send(preparator);
    		return true;
    		}
    		catch (Exception e) {
				logger.error(e.getMessage(),e);
				return false;
			}
	}
	
	public boolean sendMailNotification(String[] toEmailArray,String[] toCCArray,String emailBody, String emailSubject){
		logger.debug("inside method(sendMailNotification)");
		try{
			MailMessagePreparator mailMessagePreparator = new MailMessagePreparator(toEmailArray,toCCArray,emailBody,emailSubject);
			this.mailSender.send(mailMessagePreparator);
			logger.debug("Mail sent successfully");
		}catch (Exception e) {
			logger.error(e.getMessage(),e);
			return false;
		}
		return true;
	}
	
	public class MailMessagePreparator implements MimeMessagePreparator {

		private final String[] toEmailArray;
		private final String[] ccEmailArray;
		private final String emailBody;
		private final String emailSubject;
		
		public MailMessagePreparator(String[] toEmailArray,String[] toCCArray,String emailBody, String emailSubject){
			logger.debug("inside constructor(MailMessagePreparator)");
			this.toEmailArray = toEmailArray;
			this.ccEmailArray = toCCArray;
			this.emailBody = emailBody;
			this.emailSubject = emailSubject;
		}
		
		public void prepare(MimeMessage mimeMessage) throws Exception {
			logger.debug("inside class MailMessagePreparator: method(prepare)");
			MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage);
			
    		messageHelper.setTo(toEmailArray);
    		if(null != ccEmailArray && ccEmailArray.length > 0)
    			messageHelper.setCc(ccEmailArray);
    		
    		messageHelper.setFrom("DMSAdministrator");
    		messageHelper.setText(emailBody,true);
    		messageHelper.setSubject(emailSubject);
			
		}

		public String[] getToEmailArray() {
			return toEmailArray;
		}

		public String[] getCcEmailArray() {
			return ccEmailArray;
		}

		public String getEmailBody() {
			return emailBody;
		}

		public String getEmailSubject() {
			return emailSubject;
		}
		
	}
}
